// pages/real/videoDetails/index.js
const app = getApp()
const utils = require('../../../utils/util.js')
const doublebed='../../../static/img/real/doublebed.png'
const table='../../../static/img/real/table.png'
const singlebed='../../../static/img/real/singlebed.png'
const toilet='../../../static/img/real/toilet.png'
const cabinet='../../../static/img/real/cabinet.png'
const wardrobe='../../../static/img/real/wardrobe.png'
const door='../../../static/img/real/door.png'
const balcony='../../../static/img/real/balcony.png'
const modules='../../../static/img/real/module.png'
const firewall='../../../static/img/real/firewall.png'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailList:{},
    showSpace:false,
    showPrivacyPopup:false,
    showFullScreenFlag:false,
    tagList:[],
    system:0,
    detailId:"",
    scene:"",
    isIphoneX: app.globalData.isIphoneX,
    navTop:wx.getMenuButtonBoundingClientRect().top,
    menuHeight:app.globalData.menuHeight,  
    toTop:app.globalData.menuHeight+wx.getMenuButtonBoundingClientRect().top,
    showAuthTransition:false,
    textName:"展开更多",
    topNavHeight:app.globalData.navHeight,
    topNavHeighta:app.globalData.navHeight-app.globalData.statusBarHeight,
    statusBarHeight:app.globalData.statusBarHeight,
    videoUrl:"https://filedev.quanyou.com.cn/marketing/group2/M00/00/B8/rB8kQWI5bTKAQbkKAA1oTfH7AiA625.mp4",
    userInfo: '',
    iconList:{
     '202':doublebed ,
     '201':table,
     '204':singlebed,
     '209':toilet,
     '206':cabinet,
     '208':wardrobe,
     '200':door,
     '210':firewall,
     '205':modules,
     '207':balcony
  },
    spaceList:[
      // {
      //   id:1,
      //   title:'书房',
      //   flag:true,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:2,
      //   title:'客厅',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:3,
      //   title:'餐厅',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:4,
      //   title:'主卧',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:5,
      //   title:'阳台',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:6,
      //   title:'主卧',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // },
      // {
      //   id:7,
      //   title:'阳台',
      //   flag:false,
      //   url:'https://t7.baidu.com/it/u=4162611394,4275913936&fm=193&f=GI'
      // }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = wx.getStorageSync("userInfo")
    if(userInfo){
      this.setData({showPrivacyPopup:true})
    }
    const phone = wx.getStorageSync('phone')
    
    const {  id , scene=null} = options
    this.setData({  userInfo,  phone,   detailId:id,  scene })
    this.initData()
    
  },
  initData() {
    if(!this.data.userInfo){
      this.toLoginPage()
    } else {
      this.initDetailList()
    }
    setTimeout(()=>{
      if(!this.data.userInfo){
        wx.hideShareMenu({
          menus: ['shareAppMessage', 'shareTimeline']
        })
      }
    },200)
  },
  initDetailList() {
    if(this.data.scene) {
      const unionid = wx.getStorageSync("unionid")
      const str= { code: this.data.scene,  createBy: unionid }
      this.hendleGetCodeList(str)
    } else {
      this.handleGetDetialList()
    }
  },
  /**
   * 获取本地用户信息
  */
  getLocalUserInfo: function(){
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo){
      this.setData({ userInfo, phone: userInfo.phone })
    }
    this.initData()
  },
  toLoginPage(){
    const {userInfo , phone,scene,detailId } = this.data
    let message = ''
    if(!userInfo) {
      message = '未登陆，是否跳到登陆页登陆'
    }
    wx.showModal({
      title: '提示',
      content: message,
      success:function(res) {
        if(res.confirm){
          wx.navigateTo({
            url: '/pages/mine/register/index'
          });
        }else if (res.cancel) {
          wx.switchTab({
            url: '/pages/real/homepage/index'
          });
        }
      }
    })
  },
  showSpaceList(){
    this.setData({
      showSpace:true
    })
  },
  hanldeCloseSpace(){
    this.setData({
      showSpace:false
    })
  },
  hendleGetCodeList(str){
    utils.$http(app.globalData.api.getRealContriGetByCode, str, 'POST', true, 'JSON').then(res => {
      if(res.success){
        const s=res.data.content
        let newSpace=[]
        if(res.data.contributeType==='3' && res.data.spaceDTOList && res.data.spaceDTOList.length){
           newSpace=res.data.spaceDTOList.map((item,index)=>{
             item.flag=false
             if(index===0) item.flag=true
            for(let i in this.data.iconList){
              if(i===item.spaceCode){
                item.iconUrl=this.data.iconList[i]
              }
            }
            return item
          })
          res.data.videoUrl=res.data.spaceDTOList[0].realityImageList[0].video
        }
        this.setData({
         detailList:res.data,
         spaceList:newSpace,
         tagList:s.length>0?s.slice(0,-1).split('.').map((item,index)=>{
           const str={
             id:index,
             flag:false,
             name:item.replace(/,/g, ' , ')
           }
           return str
         }):[],
       })
      //  console.log(this.data.tagList)
      }
    })
  },
  selectSpaceItem(e){
    // console.log(e)
    const item=e.currentTarget.dataset.item
    const imgList=item.realityImageList[0].video
    const {spaceList,detailList}=this.data
   const list= spaceList.map(s=>{
     if(s.id===item.id){
       s.flag=true
     }else{
       s.flag=false
     }
     return s
   })
   detailList.videoUrl=imgList
   this.setData({
     spaceList:list,
     detailList
   })
  },
  handleGetDetialList(){
    const userInfo=wx.getStorageSync('userInfo')
    const unionid=wx.getStorageSync("unionid")
    const str={
      id:this.data.detailId,
      createBy:unionid
    }
    utils.$http(app.globalData.api.getRealContribDetail, str, 'POST', true, 'JSON').then(res => {
      if(res.success){
        const s=res.data.content
        let newSpace=[]
        if(res.data.contributeType==='3' && res.data.spaceDTOList && res.data.spaceDTOList.length){
           newSpace=res.data.spaceDTOList.map((item,index)=>{
             item.flag=false
             if(index===0) item.flag=true
            for(let i in this.data.iconList){
              if(i===item.spaceCode){
                item.iconUrl=this.data.iconList[i]
              }
            }
            return item
          })
          res.data.videoUrl=res.data.spaceDTOList[0].realityImageList[0].video
        }
           this.setData({
            detailList:res.data,
            spaceList:newSpace,
            tagList:s.length>0?s.slice(0,-1).split('.').map((item,index)=>{
              const str={
                id:index,
                flag:false,
                name:item.replace(/,/g, ' , ')
              }
              return str
            }):[],
          })
          // console.log(this.data.tagList)
      }
    }) 
  },
  showAllTextContent(){
    this.setData({
      showAuthTransition:!this.data.showAuthTransition
    })
    if(this.data.showAuthTransition){
      this.setData({
        textName:"收起"
      })
    }else{
      this.setData({
        textName:"展开更多"
      })
    }
  },
  showFullScreen(){
    const videoContext = wx.createVideoContext('myVideo');// 	创建 video 上下文 VideoContext 对象。
    videoContext.requestFullScreen({	// 设置全屏时视频的方向，不指定则根据宽高比自动判断。
        direction: 90						// 屏幕逆时针90度
      });
  },
  watchVideoIsFull(e){
    if(e.detail.fullscreen){
      this.setData({
        showFullScreenFlag:true
      })
    }else{
      this.setData({
        showFullScreenFlag:false
      })
    }
  },
  closeLowBox(){
    this.setData({
      showAuthTransition:false,
      textName:"展开更多"
    })
  },
  goBack(){
    wx.navigateBack({
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const sharerPhone = wx.getStorageSync('phone') || ''
    const sharerOpenId = wx.getStorageSync('openId') 

    const imageUrl = this.data.detailList.realityPic && utils.filterURL(this.data.detailList.realityPic) 

    return {
      title: this.data.detailList.realityName || '详情页',
      imageUrl: imageUrl,
      path: "pages/real/videoDetails/index?id=" + this.data.detailList.id + "&sharerPhone=" + sharerPhone + "&sharerOpenId=" + sharerOpenId
    }
  }
})